<template>
  <div>
    <el-button size="mini" @click="addGj">新增</el-button>
    <el-button size="mini" @click="toExcel">导出excel</el-button>
    <el-table :data="$store.state.guojijibing.GjData" style="width: 100%">
      <el-table-column align="center" label="ICD-9编码">
        <template slot-scope="scope">
          <span>{{ scope.row.diseaseIcd9 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="疾病名称">
        <template slot-scope="scope">
          <i class="el-icon-user"></i>
          <span>{{ scope.row.diseaseName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="拼音码">
        <template slot-scope="scope">
          <span>{{ scope.row.diseaseCode }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="疾病分类码">
        <template slot-scope="scope">
          <span>{{ scope.row.diseaseType }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="170px">
        <template slot-scope="scope">
          <el-button size="mini" @click="updataGj(scope.row)">修改</el-button>
          <el-button size="mini" @click="delectGj(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination layout="prev, pager, next" :page-count="$store.state.guojijibing.maxPage" @current-change="handleCurrentChange"></el-pagination>
    </div>
    <el-dialog :title="myTitle" :visible.sync="updataVisible" :modal="false">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="ICD-9编码" :label-width="formLabelWidth" prop="diseaseIcd9">
          <el-input v-model="form.diseaseIcd9" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="疾病名称" :label-width="formLabelWidth" prop="diseaseName">
          <el-input v-model="form.diseaseName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="拼音码" :label-width="formLabelWidth" prop="diseaseCode">
          <el-input v-model="form.diseaseCode" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="疾病分类码" :label-width="formLabelWidth" prop="diseaseType">
          <el-input v-model="form.diseaseType" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="myTitle == '修改疾病'">
        <el-button @click="updataVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataGjQd">确 定</el-button>
      </div>
      <div slot="footer" class="dialog-footer" v-if="myTitle == '新增疾病'">
        <el-button @click="updataVisible = false">取 消</el-button>
        <el-button type="primary" @click="addGjQd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import tishi from '../../../mixins/tishi';
import { mapActions } from 'vuex';

export default {
  mixins: [tishi],
  data() {
    return {
      nowPage: 1,
      updataVisible: false,
      form: {},
      addform: {},
      formLabelWidth: '100px',
      rules: {
        diseaseIcd9: { required: true, message: '请输入ICD-9编码', trigger: 'blur' },
        diseaseName: { required: true, message: '请输入疾病名称', trigger: 'blur' },
        diseaseCode: { required: true, message: '请输入拼音码', trigger: 'blur' },
        diseaseType: { required: true, message: '请输入疾病分类码', trigger: 'blur' },
      },
      myTitle: '',
    };
  },
  watch: {
    updataVisible: function() {
      if (this.updataVisible == false) this.$refs['form'].clearValidate();
    },
  },
  created() {
    this.seletGjData(1);
  },
  methods: {
    ...mapActions(['tongyong', 'guojijibing/selectGjData']),
    seletGjData(nowPage) {
      this['guojijibing/selectGjData']({
        name: 'SELECT_GJALLDATA',
        data: {
          page: nowPage,
          pageSize: 5,
        },
      });
    },
    handleCurrentChange(val) {
      this.nowPage = val;
      this.seletGjData(val);
    },
    updataGj(data) {
      this.updataVisible = true;
      this.form = { ...data };
      this.myTitle = '修改疾病';
    },
    updataGjQd() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          //通过验证执行
          this['tongyong']({
            name: 'UPDATA_GJ',
            data: this.form,
          }).then(resp => {
            if (resp.data.code == 200) {
              this.tankuang('修改', resp.data.code);
              this.seletGjData(this.nowPage);
              this.updataVisible = false;
            }
          });
        } else {
          //验证失败执行
          this.$message({
            type: 'error',
            message: '请补全信息',
          });
          return false;
        }
      });
    },
    delectGj(data) {
      this.deleteDataTy(
        {
          name: 'DELECT_GJ',
          data: data,
        },
        '删除'
      ).then(resp => {
        if (resp) {
          if (resp.data.code == 200) {
            this.seletGjData(this.nowPage);
          }
        }
      });
    },
    addGj() {
      this.form = {};
      this.updataVisible = true;
      this.myTitle = '新增疾病';
    },
    addGjQd() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          //通过验证执行
          this['tongyong']({
            name: 'ADDDATA_GJ',
            data: this.form,
          }).then(resp => {
            if (resp.data.code == 200) {
              this.tankuang('新增', resp.data.code);
              this.seletGjData(this.nowPage);
              this.updataVisible = false;
            }
          });
        } else {
          //验证失败执行
          this.$message({
            type: 'error',
            message: '请补全信息',
          });
          return false;
        }
      });
    },
    toExcel() {
      this['tongyong']({
        name: 'EXCEL_TOEXCEL',
      }).then(resp => {
        this.tankuang('导出', resp.data.code);
      });
    },
  },
};
</script>
<style lang="less" scoped></style>
